package ter.find.pattern.algoFind;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.List;

import ter.find.pattern.Base;
import ter.find.pattern.Motif;
	
	

public class RunSearch {
	
	
	public static String fileToPath(String filename) throws UnsupportedEncodingException{
		URL url = RunSearch.class.getResource(filename);
		 return java.net.URLDecoder.decode(url.getPath(),"UTF-8");
	}
	
	public List<Motif> start(int support,String file,List<Integer> list){
		SequenceDatabase sequenceDatabase = new SequenceDatabase(); 
		try {
			sequenceDatabase.loadFile(file);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Affichage des données
		sequenceDatabase.print();
		if(list.isEmpty()){
			double val=support*0.01;
			// Create an instance of the algorithm with minsup = 50 %
			System.out.println("\n "+val+" \n");
			PrefixSpan algo = new PrefixSpan(val); 
			
			// execute the algorithm
			algo.runAlgorithm(sequenceDatabase);
			//algo.printStatistics(sequenceDatabase.size());
			
			
			return algo.getAll(sequenceDatabase.size());
		}else{
			sequenceDatabase.setSelectedSequence(sequenceDatabase.getSelectedSequence(list));
			double val=support*0.01;
			// Create an instance of the algorithm with minsup = 50 %
			System.out.println("\n "+val+" \n");
			PrefixSpan algo = new PrefixSpan(val); 
			
			// execute the algorithm
			algo.runAlgorithm(sequenceDatabase);
			//algo.printStatistics(sequenceDatabase.size());
			return algo.getAll(sequenceDatabase.size());
		}
		
		
		
		//return algo.getAll(sequenceDatabase.size());
		
	
	}
	
	public List<Base> getAll(String path){
		SequenceDatabase sequenceDatabase = new SequenceDatabase(); 
		try {
			sequenceDatabase.loadFile(path);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Affichage des données
		return sequenceDatabase.loadBaseSeq();
	}
	
	
	public void saveAllToFile(String toFile,String source) throws IOException{
		SequenceDatabase sequenceDatabase = new SequenceDatabase(); 
		sequenceDatabase.loadFile(fileToPath(source));

		Prefix_Save algo = new Prefix_Save(); 
		
		int minsup = 2; // we use a minimum support of 2 sequences.
		
		// execute the algorithm
		algo.runAlgorithm(sequenceDatabase, toFile, minsup);    
		
	}

}
